//
//  RegisterViewController.m
//  QSBK
//
//  Created by Eric Wang on 5/7/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//
#import "AFNetworking.h"
#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>
- (IBAction)back:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *noticeLabel;
@property (weak, nonatomic) IBOutlet UIImageView *refreshImageView;
@property (weak, nonatomic) IBOutlet UITextField *countTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet UITextField *repwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelbuttton;
- (IBAction)registerCount:(id)sender;
- (IBAction)cancel:(id)sender;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubviews];
    // Do any additional setup after loading the view from its nib.
}

-(void)setSubviews
{
    self.refreshImageView.hidden = YES;
    self.noticeLabel.hidden = YES;
    self.registerButton.backgroundColor = [UIColor colorWithRed:114/255.0 green:211/255.0 blue:109/255.0 alpha:1.0];
    [self.registerButton.layer setCornerRadius:5];
    self.cancelbuttton.backgroundColor = [UIColor colorWithRed:114/255.0 green:211/255.0 blue:109/255.0 alpha:1.0];
    [self.cancelbuttton.layer setCornerRadius:5];
    
    self.countTextField.delegate = self;
    self.pwdTextField.delegate = self;
    self.repwdTextField.delegate = self;
}

-(BOOL)checkText
{
    if(self.countTextField.text ==nil)
    {
        return NO;
    }else if([self.pwdTextField.text isEqualToString:@""]||![self.pwdTextField.text isEqualToString:self.repwdTextField.text])
    {
        return NO;
    }else{
        return YES;
    }
}

- (IBAction)registerCount:(id)sender {
    
    if(![self checkText])
    {
        self.noticeLabel.text=@"请检查用户名或者密码!";
        self.noticeLabel.hidden = NO;
        [self performSelector:@selector(hideNoticeLabel) withObject:nil afterDelay:1.5];
    }else{
        
        [self addCount];
        CABasicAnimation* rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
        rotationAnimation.duration = 1.5;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = MAXFLOAT;
        
        [self.refreshImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
}

-(void)addCount
{
    NSString *urlString = @"http://localhost:8888/thinkphp_3.2.3/Home/QSBK/register?";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *para = @{@"user_count":self.countTextField.text,@"user_pwd":self.pwdTextField.text};
    [manager POST:urlString parameters:para success:^(AFHTTPRequestOperation *operation,id responseObject)
    {
        NSDictionary *dic = (NSDictionary *)responseObject;
        
        NSString *str = [dic objectForKey:@"statu"];
        NSString *errStr = [dic objectForKey:@"err"];
        
        if([errStr isEqualToString:@"0"])
        {
            [self performSelector:@selector(cancel:) withObject:nil afterDelay:1.6];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            self.noticeLabel.text = str;
            self.noticeLabel.hidden = NO;
            [self performSelector:@selector(hideNoticeLabel) withObject:nil afterDelay:1.5];
            [self.refreshImageView.layer removeAllAnimations];
            self.refreshImageView.hidden = YES;
        });
        
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
        NSLog(@"register..%@",error);
    }];
}
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)hideNoticeLabel
{
    self.noticeLabel.hidden = YES;
    
}

#pragma textField delegate 
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
